﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Remember.WPF.Views;
using EZMemoLib;
using System.ComponentModel;

namespace Remember.WPF.Pages
{
    /// <summary>
    /// Interaction logic for BookListPage.xaml
    /// </summary>
    public partial class BookListPage : Page
    {
        public BookListPage()
        {
            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                Bind();
            }
        }

        private void Bind()
        {
            ShowBookList();
        }

        void ShowBookList()
        {
            this.bookListControl1.OnClickBook += new EventHandler(bookListControl1_OnClickBook);
            this.bookListControl1.OnClickEdit += new EventHandler(bookListControl1_OnClickEdit);
            this.bookListControl1.OnStart += new EventHandler(bookListControl1_OnStart);
            this.bookListControl1.OnSwitchUser += new EventHandler(bookListControl1_OnSwitchUser);

            this.bookListControl1.Build();
        }

        void bookListControl1_OnSwitchUser(object sender, EventArgs e)
        {
            var loginPage = new Uri(MainWindow.LoginPageUrl , System.UriKind.Relative);
            this.NavigationService.Navigate(loginPage);
        }

        void bookListControl1_OnStart(object sender, EventArgs e)
        {
            long firstBookid = 0 ;
            string bookIDString = sender as string;

            long.TryParse(bookIDString, out firstBookid);

            if (firstBookid > 0)
            {
                this._CurrentBookID = firstBookid;
                StartBook(firstBookid);
            }
        }

        void bookListControl1_OnClickBook(object sender, EventArgs e)
        {
            string bookIDString = sender as string;
            long bookid = 0;
            long.TryParse(bookIDString, out bookid);

            StartBook( bookid);
        }

        void bookListControl1_OnClickEdit(object sender, EventArgs e)
        {
            string bookIDString = sender as string;
            int bookID = 0;
            int.TryParse(bookIDString, out bookID);

            //bookID > 0 - edit; bookID <= 0 - add
            OpenBookEditWindow(bookID);
        }

        void OpenBookEditWindow( int bookID)
        {
            var bookEdit1 = new BookEdit();
            bookEdit1.OnClose += new EventHandler((a, b) => { this.childWindow.Close(); Bind(); });

            if (bookID > 0)
            {
                bookEdit1.LoadBook(bookID);
            }

            childWindow.Content = bookEdit1;

            childWindow.WindowStartupLocation = Microsoft.Windows.Controls.WindowStartupLocation.Center;

            if (bookEdit1.Height > 0)
            {
                childWindow.Height = bookEdit1.Height + 40;
            }

            if (bookEdit1.Width > 0)
            {
                childWindow.Width = bookEdit1.Width;
            }
            childWindow.Show();

        }


        void StartBook(long bookID)
        {
            var page = new PlayerPage(bookID);
            page.OnFinished += new EventHandler(page_OnFinished);

            if (this.NavigationService != null)
            {
                this.NavigationService.Navigate(page);
            }
            else
            {
                if (nav != null)
                { 
                    nav.Navigate(page);
                }
            }

            //this.NavigationService.Navigate(new Uri("/Pages/PlayerPage.xaml?cid=" + bookID , UriKind.Relative  )); 

        }

        NavigationService nav;
        void page_OnFinished(object sender, EventArgs e)
        {
            nav = (sender as Page).NavigationService;

            StartNext();
        }

        long _CurrentBookID = -1;
        void StartNext()
        {
            var nextbook = this.bookListControl1.GetNextBook(_CurrentBookID);
            if (nextbook != null)
            {
                _CurrentBookID = nextbook.CategoryID;
                StartBook(nextbook.CategoryID);
            }
            else
            {
                var homePage = new Uri(MainWindow.HomePageUrl, System.UriKind.Relative );
                if (this.NavigationService != null)
                {
                    this.NavigationService.Navigate(homePage);
                }
                else
                {
                    if (nav != null)
                    {
                        nav.Navigate(homePage);
                    }
                }
            }
        }


    }
}
